From 1c5885240cbaccd2cfa99db25ed9bb589aa03934 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Thu, 8 Jun 2017 12:39:28 +0200 Subject: [PATCH] Factor search for sibling Cargo.toml and .git folder into separate subroutine. --- src/cargo/sources/path.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/cargo/sources/path.rs b/src/cargo/sources/path.rs index 442524df1..cb9c2dde0 100644 --- a/src/cargo/sources/path.rs +++ b/src/cargo/sources/path.rs @@ -114,6 +114,20 @@ impl<'cfg> PathSource<'cfg> { } }; + if let Some(result) = self.discover_git_and_list_files(pkg, root, &mut filter) { + return result; + } + + self.list_files_walk(pkg, &mut filter) + } + + // Returns Some(_) if found sibling Cargo.toml and .git folder; + // otherwise caller should fall back on full file list. + fn discover_git_and_list_files(&self, + pkg: &Package, + root: &Path, + filter: &mut FnMut(&Path) -> bool) + -> Option>> { // If this package is in a git repository, then we really do want to // query the git repository as it takes into account items such as // .gitignore. We're not quite sure where the git repository is, @@ -129,11 +143,14 @@ impl<'cfg> PathSource<'cfg> { // check to see if we are indeed part of the index. If not, then // this is likely an unrelated git repo, so keep going. if let Ok(repo) = git2::Repository::open(cur) { - let index = repo.index()?; + let index = match repo.index() { + Ok(index) => index, + Err(err) => return Some(Err(err.into())), + }; let path = util::without_prefix(root, cur) .unwrap().join("Cargo.toml"); if index.get_path(&path, 0).is_some() { - return self.list_files_git(pkg, repo, &mut filter); + return Some(self.list_files_git(pkg, repo, filter)); } } } @@ -146,7 +163,7 @@ impl<'cfg> PathSource<'cfg> { None => break, } } - self.list_files_walk(pkg, &mut filter) + return None; } fn list_files_git(&self, pkg: &Package, repo: git2::Repository, -- 2.30.2